#pragma once

#include <iostream>
#include <functional>
#include <cstdio>

class Task
{
    using func_t = std::function<int(int, int, char)>;
public:
    Task() {}

    Task(int x, int y, char op, func_t callback) 
    : _x(x), _y(y), _op(op), _callback(callback)
    {}

    std::string operator ()()
    {
        int result = _callback(_x, _y, _op);//跳转到上层传入的函数的位置
        char buffer[1024];
        snprintf(buffer, sizeof(buffer), "%d %c %d = %d", _x, _op, _y, result);
        return buffer;
    }

    //方便将生产出的计算进行输出
    std::string toTaskString()
    {
        char buffer[1024];
        snprintf(buffer, sizeof(buffer), "%d %c %d = ?", _x, _op, _y);
        return buffer;
    }

    ~Task() {}

private:
    int _x;
    int _y;
    int _op;
    func_t _callback;//回调方法
};

const std::string oper = "+-*/%";
int mymath(int x, int y, char op)
{
    int result = 0;
    switch (op)
    {
    case '+':
        result = x + y;
        break;
    case '-':
        result = x - y;
        break;
    case '*':
        result = x * y;
        break;
    case '/':
    {
        if (y == 0)
        {
            std::cerr << "div zero error!" << std::endl;
            return -1;
        }
        else result = x / y;
    }
        break;
    case '%':
    {
        if (y == 0)
        {
            std::cerr << "mod zero error!" << std::endl;
            return -1;
        }
        else result = x % y;
    }
        break;
    default:
        break;
    }

    return result;
}
